import os
import glob
import cv2
import pandas as pd
from pathlib import Path


def img_check(img_path, no_defect_code, label_suffix):
    code = Path(img_path).parent.name
    if code == no_defect_code:
        return
    label_path = Path(img_path).with_suffix(label_suffix)
    if not label_path.exists():
        os.remove(img_path)
    else:
        try:
            img = cv2.imread(img_path)
            shape = img.shape
        except:
            os.remove(img_path)


def label_check(label_path, img_suffix):
    img_path = Path(label_path).with_suffix(img_suffix)
    if not img_path.exists():
        os.remove(label_path)


def main():
    try:
        from pandarallel import pandarallel
        pandarallel.initialize(progress_bar=True) 
        print('Use multi threading !')
        is_pandarallel = True
    except:
        print('Use single threading !')
        is_pandarallel = False

    path =r'/data2/autorepair/ruanzhifeng/autorepair_t7_10/t7/T6008/all_0416'
    no_defect_code = 'TGXID'
    label_suffix = '.json'
    img_suffix = '.jpg'

    df = pd.DataFrame()
    df['img_path'] = glob.glob(os.path.join(path, '*/*'+img_suffix))
    df['img_path'].parallel_apply(lambda x: img_check(x, no_defect_code, label_suffix))
    print()
    df = pd.DataFrame()
    df['label_path'] = glob.glob(os.path.join(path, '*/*'+label_suffix))
    df['label_path'].parallel_apply(lambda x: label_check(x, img_suffix))
    print()


if __name__=='__main__':
    main()
